package jfox.platform.aaa.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.aaa.entity.User;
import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;

/**
 * @author <a href="mailto:thaiguo@gmail.com">Taiguo Zhang</a>
 */
@Stateless
@Local
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = UserDAOBean.GET_USER_BY_ID,
                query = "SELECT * FROM T_SYS_USER WHERE ID=$ID AND VSTATUS = " + EntityObject.VSTATUS_NORMAL,
                resultClass = User.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "user")
                }
        ),
        @NamedNativeQuery(
                name = UserDAOBean.GET_USER_BY_NAME,
                query = "SELECT * FROM T_SYS_USER WHERE NAME=$NAME AND VSTATUS = " + EntityObject.VSTATUS_NORMAL,
                resultClass = User.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "user")
                }
        ),
        @NamedNativeQuery(
                name = UserDAOBean.GET_USER_BY_NAME_AND_PASSWORD,
                query = "SELECT * FROM T_SYS_USER WHERE NAME=$NAME AND PASSWORD=$PASSWORD AND VSTATUS = " + EntityObject.VSTATUS_NORMAL,
                resultClass = User.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "user")
                }
        ),
        @NamedNativeQuery(
                name = UserDAOBean.GET_ALL_USERS,
                query = "SELECT * FROM T_SYS_USER WHERE VSTATUS=" + EntityObject.VSTATUS_NORMAL + " AND VSTATUS != " + EntityObject.VSTATUS_REMOVED,
                resultClass = User.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "user")
                }
        ),
        @NamedNativeQuery(
                name = UserDAOBean.INSERT_USER,
                query = "INSERT INTO T_SYS_USER(" +
                        "ID," +
                        "NAME, " +
                        "PRIORITY, " +                       
                        "VSTATUS, " +
                        "CREATE_TIME, " +
                        "CREATOR, " +
                        "LAST_MODIFIED, " +
                        "LAST_MODIFIER, " +                        
                        "DESCRIPTION, " +
                        "REFS, " +
                        "FIRST_NAME, " +
                        "LAST_NAME, " +
                        "PASSWORD, " +
                        "RESTRICT_IP, " + 
                        "GENDER, " + 
                        "BIRTHDAY, " +
                        "EMAIL, " + 
                        "MOBILE, " +
                        "PHONE, " +
                        "POST_ADDRESS, " +
                        "POST_CODE, " +
                        "PICTURE, " +
                        "GRADUATE_SCHOOL, " +
                        "GRADUATE_YEAR, " +
                        "EDUCATION_DEGREE, " +
                        "SPECIALITY, " +
                        "POSITION, " +
                        "LAST_LOGIN, " +
                        "LANGUAGE, " +
                        "THEME, " +
                        "ROLES, " +
                        "NODES, " +
                        "DEPARTMENT_ID ) " +
                        "VALUES (" +
                        "$USER.getId()," +
                        "$USER.getName()," +
                        "$USER.getPriority()," +
                        "$USER.getVstatus()," +
                        "$USER.getCreateTime()," +
                        "$USER.getCreator()," +
                        "$USER.getLastModified()," +
                        "$USER.getLastModifier()," +                        
                        "$USER.getDescription()," +
                        "$USER.getRefs()," +
                        "$USER.getFirstName()," +
                        "$USER.getLastName()," +
                        "$USER.getPassword()," +
                        "$USER.getRestrictIp()," +
                        "$USER.getGender()," +
                        "$USER.getBirthday()," +
                        "$USER.getEmail()," +
                        "$USER.getMobile()," +
                        "$USER.getPhone()," +
                        "$USER.getPostAddress()," +
                        "$USER.getPostCode()," +
                        "$USER.getPicture()," +
                        "$USER.getGraduateSchool()," +
                        "$USER.getGraduateYear()," +
                        "$USER.getEducationDegree()," +
                        "$USER.getSpeciality()," +
                        "$USER.getPosition()," +
                        "$USER.getLastLogin()," +
                        "$USER.getLanguage()," +
                        "$USER.getTheme()," +
                        "$USER.getRoles()," +
                        "$USER.getNodes()," +
                        "$USER.getDepartmentId()" +     
                        ")",
                        hints = {
                            	@QueryHint(name = "cache.partition", value = "user")
                            }
        ),
        @NamedNativeQuery(
                name = UserDAOBean.DELETE_USER,
                query = "UPDATE T_SYS_USER SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE ID=$ID",
                resultClass = User.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "user")
                }
        ),
        @NamedNativeQuery(
        		name = UserDAOBean.UPDATE_USER,
        		query = "UPDATE T_SYS_USER SET " +
        			"NAME = $USER.getName(), " +
                    "PRIORITY = $USER.getPriority(), " +                       
                    "VSTATUS = $USER.getVstatus(), " +
                    "CREATE_TIME = $USER.getCreateTime(), " +
                    "CREATOR = $USER.getCreator(), " +
                    "LAST_MODIFIED = $USER.getLastModified(), " +
                    "LAST_MODIFIER = $USER.getLastModifier(), " +                        
                    "DESCRIPTION = $USER.getDescription(), " +
                    "REFS = $USER.getRefs(), " +
                    "FIRST_NAME = $USER.getFirstName(), " +
                    "LAST_NAME = $USER.getLastName(), " +
                    "PASSWORD = $USER.getPassword(), " +
                    "RESTRICT_IP = $USER.getRestrictIp(), " + 
                    "GENDER = $USER.getGender(), " + 
                    "BIRTHDAY = $USER.getBirthday(), " +
                    "EMAIL = $USER.getEmail(), " + 
                    "MOBILE = $USER.getMobile(), " +
                    "PHONE = $USER.getPhone(), " +
                    "POST_ADDRESS = $USER.getPostAddress(), " +
                    "POST_CODE = $USER.getPostCode(), " +
                    "PICTURE = $USER.getPicture(), " +
                    "GRADUATE_SCHOOL = $USER.getGraduateSchool(), " +
                    "GRADUATE_YEAR = $USER.getGraduateYear(), " +
                    "EDUCATION_DEGREE = $USER.getEducationDegree(), " +
                    "SPECIALITY = $USER.getSpeciality(), " +
                    "POSITION = $USER.getPosition(), " +
                    "LAST_LOGIN = $USER.getLastLogin(), " +
                    "LANGUAGE = $USER.getLanguage(), " +
                    "THEME = $USER.getTheme(), " +
                    "ROLES = $USER.getRoles(), " +
                    "NODES = $USER.getNodes(), " +
                    "DEPARTMENT_ID = $USER.getDepartmentId() " +
                    "WHERE ID=$USER.getId()" ,
                    resultClass = User.class,
                    hints = {
                    	@QueryHint(name = "cache.partition", value = "user")
                    }
        )        
        }
)
public class UserDAOBean extends SuperDAO implements UserDAO {

    public static final String GET_USER_BY_ID = "UserDAOBean.getUserById";
    public static final String GET_USER_BY_NAME = "UserDAOBean.getUserByName";
    public static final String GET_USER_BY_NAME_AND_PASSWORD = "UserDAOBean.getUserByNameAndPassword";
    public static final String GET_ALL_USERS = "UserDAOBean.getAllUsers";
    public final static String INSERT_USER = "UserDAOBean.insertUser";
    public final static String DELETE_USER = "UserDAOBean.deleteUser";
    public final static String UPDATE_USER = "UserDAOBean.updateUser";

    public User getUserById(long id) {
        return (User)getEntityObjectByColumn(GET_USER_BY_ID, "ID", id);
    }

    public User getUserByName(String name) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("NAME", name);
        return (User)processNamedNativeQuerySingle(GET_USER_BY_NAME,params);

    }

    public User getUserByNameAndPassword(String username, String password) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("NAME", username);
        params.put("PASSWORD", password);
        return (User)processNamedNativeQuerySingle(GET_USER_BY_NAME_AND_PASSWORD,params);
    }

    public void insertUser(User user) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("USER", user);
        executeNamedNativeUpdate(INSERT_USER, params);
    }

    public List<User> getAllUsers() {
        return (List<User>)processNamedNativeQuery(GET_ALL_USERS, null);
    }

    public int deleteUser(long userId) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("ID", userId);
        return executeNamedNativeUpdate(DELETE_USER,params);
    }


	public int updateUser(User user) {
		Map<String, Object> params = new HashMap<String, Object>(1);
		params.put("USER", user);
		return executeNamedNativeUpdate(UserDAOBean.UPDATE_USER,params);
	}


}
